package br.com.poolme.telas;

import java.io.IOException;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.TiledLayer;

public class GameScreen extends GameCanvas{

	private TiledLayer background;
	private LayerManager manager;
	private Image tiles;

	public static int X0MESA;
	public static int Y0MESA;
	public static int WMESA;
	public static int HMESA;

	int[] cells = null;

	private static GameScreen instance;
	private Command voltar;

	private Graphics graphics;

	private Bola[] bolas;
	public Bola[] getBolas(){
		return this.bolas;
	}

	private Thread[] todasThreads;
	public Thread getMinhaThread(Bola bola){
		for(int i=0; i< this.bolas.length ; i++){
			if(this.bolas[i].equals(bola)){
				return todasThreads[i];
			}
		}
		return null;
	}

	public static GameScreen getInstance(){
		if (instance == null){
			//ver a questao desse boolean
			instance = new GameScreen(true);
		}
		return instance;
	}
	protected GameScreen(boolean suppressKeyEvents) {
		super(suppressKeyEvents);

		this.graphics = getGraphics();
		this.createBackGround();
		manager = new LayerManager();
		voltar = new Command("Sair", Command.BACK, 0);
		this.addCommand(voltar);
		//primeiro adiciona bolas de sinuca
		adicionaBolas();
		//depois coloca o background na fila
		manager.append(background);
		drawScreen();
	}

	private void drawScreen() {
		this.graphics.setColor(0xfffffff);
		this.graphics.fillRect(0, 0, getWidth(), getHeight());
		this.graphics.setColor(0xffffffff);
		flushGraphics();
		manager.paint(this.graphics, 0, 0);
	}

	public void createBackGround() {
		try {
			tiles = Image.createImage("/tiles.png");
		} catch (IOException e) {
			e.printStackTrace();
		}

		int colunasMatriz = 5;
		int linhasMatriz = 9;

		background = new TiledLayer (colunasMatriz, linhasMatriz, tiles, 36, 30);

		//pega qntidade de espacos em branco que sobram tirando o tiled
		int temp1 = (getWidth()-(colunasMatriz*36))/2;
		int temp2 = (getHeight()-(linhasMatriz*30))/2;

		int posicaoXinicial = temp1;
		int posicaoYinicial = temp2;

		GameScreen.X0MESA = posicaoXinicial;//30;
		GameScreen.Y0MESA = posicaoYinicial;//10;
		GameScreen.WMESA = posicaoXinicial+(colunasMatriz*36);
		GameScreen.HMESA= posicaoYinicial+(linhasMatriz*30);



		//array para montagem da mesa usando os tiles
		cells = new int[] {
				1, 5, 5, 5, 6,
				2, 4, 4, 4, 7,
				2, 4, 4, 4, 7,
				2, 4, 4, 4, 7,
				3, 4, 4, 4, 8,
				2, 4, 4, 4, 7,
				2, 4, 4, 4, 7,
				2, 4, 4, 4, 7,
				10, 9, 9, 9, 11
			};

			// setando as imagens
			for (int i = 0; i < cells.length; i++) {

				int column = i % 5;
				int row = (i - column)/5;

				background.setCell(column, row, cells[i]);


			}
			//posicionando a mesa na tela
			background.setPosition(GameScreen.X0MESA,GameScreen.Y0MESA);

	}

	public Command getBotaoSair(){
		return voltar;
	}

	/*public void run() {
		graphics.setColor(255, 0, 0);
		graphics.drawLine( X0MESA+10, Y0MESA, X0MESA+10, HMESA);

		graphics.drawLine( WMESA-10, Y0MESA, WMESA-10, HMESA);

		graphics.drawLine( X0MESA, Y0MESA+6, WMESA, Y0MESA+6);

		graphics.drawLine( X0MESA, HMESA-6, WMESA, HMESA-6);
	}

	public void start() {
	  Thread t = new Thread(this);
	  t.start();
	}*/

	private void adicionaBolas(){
		int meioMesaX = ((GameScreen.X0MESA+GameScreen.WMESA)/2)-21;
		int meioMesaY = (GameScreen.Y0MESA+28);
		this.bolas = new Bola[7];
		this.todasThreads = new Thread[7];
        String cor = "branco";
        try {
            Bola branca = new Bola(cor, (meioMesaX+14), (meioMesaY+100) , this.graphics, this.manager, this);
            this.bolas[0] = branca;
            Thread thread = new Thread(branca);
            //thread.start();
            todasThreads[0] = thread;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
		cor = "preto";
		try {
            Bola preta = new Bola(cor, meioMesaX, meioMesaY , this.graphics, this.manager, this);
            this.bolas[1] = preta;
            Thread thread = new Thread(preta);
            //thread.start();
            todasThreads[1] = thread;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        cor = "azul";
        try {
            Bola azul = new Bola(cor, (meioMesaX+14)+3, (meioMesaY) , this.graphics, this.manager, this);
            this.bolas[2] = azul;
            Thread thread = new Thread(azul);
            //thread.start();
            todasThreads[2] = thread;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        cor = "verde";
        try {
            Bola verde = new Bola(cor, (meioMesaX+28)+6, meioMesaY , this.graphics, this.manager, this);
            this.bolas[3] = verde;
            Thread thread = new Thread(verde);
            //thread.start();
            todasThreads[3] = thread;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        cor = "vermelho";
        try {
            Bola vermelho = new Bola(cor, (meioMesaX+7), (meioMesaY+14)+2 , this.graphics, this.manager, this);
            this.bolas[4] = vermelho;
            Thread thread = new Thread(vermelho);
            //thread.start();
            todasThreads[4] = thread;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        cor = "rosa";
        try {
            Bola rosa = new Bola(cor, (meioMesaX+21)+3, (meioMesaY+14)+2 , this.graphics, this.manager, this);
            this.bolas[5] = rosa;
            Thread thread = new Thread(rosa);
            //thread.start();
            todasThreads[5] = thread;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        cor = "amarelo";
        try {
            Bola amarelo = new Bola(cor, (meioMesaX+14)+3, (meioMesaY+28)+2 , this.graphics, this.manager, this);
            this.bolas[6] = amarelo;
            Thread thread = new Thread(amarelo);
            //thread.start();
            todasThreads[6] = thread;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        for(int i=0;i<todasThreads.length;i++){
        	todasThreads[i].start();
        }


	}
}
